<template>
  <div class="app-container">
    <div class="filter-container">
      <el-input v-model="listQuery.keywords" placeholder="请输入内容" style="width: 200px" />
      <treeselect
        v-model="cate_id"
        class="treeselect-main"
        :options="treeOptions"
        clearable
        placeholder="选择分类"
      />
      <el-button class="filter-item" type="primary" icon="el-icon-search" style="margin-left: 10px;" @click="handleFilter">
        搜索
      </el-button>
    </div>

    <el-table
      v-loading="listLoading"
      :data="list"
      element-loading-text="Loading"
      border
      fit
      highlight-current-row
    >
      <el-table-column align="center" label="ID" width="95">
        <template slot-scope="scope">
          {{ scope.row._source.id }}
        </template>
      </el-table-column>

      <el-table-column label="标题">
        <template slot-scope="scope">
          {{ scope.row._source.title }}
        </template>
      </el-table-column>

      <el-table-column label="分类" width="180">
        <template slot-scope="scope">
          {{ scope.row._source.category_name }}
        </template>
      </el-table-column>

      <el-table-column label="创建时间" width="170">
        <template slot-scope="scope">
          {{ scope.row._source.created_at }}
        </template>
      </el-table-column>

      <el-table-column label="更新时间" width="170">
        <template slot-scope="scope">
          {{ scope.row._source.updated_at }}
        </template>
      </el-table-column>

      <el-table-column label="操作" width="160">
        <template slot-scope="{row}">
          <el-button type="primary" size="mini" @click="handleUpdate(row)">
            编辑
          </el-button>
          <el-button v-if="row.status!='deleted'" slot="reference" size="mini" type="danger" @click="handleDelete(row)">
            删除
          </el-button>
        </template>
      </el-table-column>

    </el-table>

    <pagination v-show="total>0" :total="total" :page.sync="listQuery.page" :limit.sync="listQuery.size" @pagination="fetchData" />

  </div>
</template>

<script>
import Pagination from '@/components/Pagination' // secondary package based on el-pagination
import { getCategory } from '@/api/articleCategory'
import { getList, del } from '@/api/article'

import treeselect from '@riophae/vue-treeselect'
import '@riophae/vue-treeselect/dist/vue-treeselect.css'

export default {
  name: 'ArticleList',
  components: { Pagination, treeselect },
  filters: {
    statusFilter(status) {
      const statusMap = {
        published: 'success',
        draft: 'gray',
        deleted: 'danger'
      }
      return statusMap[status]
    }
  },
  data() {
    return {
      cate_id: null,
      treeValue: null,
      treeOptions: [],
      list: null,
      total: 0,
      listLoading: true,
      listQuery: {
        page: 1,
        size: 10,
        category_id: 0,
        keywords: '',
        sort: '+id'
      }
    }
  },
  created() {
    // console.log(this.$store.getters.testName)
    this.fetchData()
  },
  methods: {
    handleFilter() {
      if (this.cate_id > 0) {
        this.listQuery.category_id = this.cate_id
      } else {
        this.listQuery.category_id = null
      }
      console.log(this.cate_id)
      this.listQuery.page = 1
      this.fetchData()
    },
    fetchData() {
      this.listLoading = true
      getList(this.listQuery).then(response => {
        this.list = response.data.result
        this.listLoading = false
        this.total = response.data.total
      })
      getCategory().then(response => {
        this.treeOptions = this.recursiveTree(response.data)
      })
    },
    handleUpdate(row) {
      console.log(row._source.id)
      this.$router.push({ path: '/article/edit/' + row._source.id })
    },
    handleDelete(row) {
      this.$confirm('确认删除？')
        .then(_ => {
          console.log(row._source.id)
          del(row._source.id).then(response => {
            if (response.code === 0) {
              this.$notify({
                title: '成功',
                message: '删除成功',
                type: 'success',
                duration: 2000
              })
              this.fetchData()
              // this.list.splice(row._source.id, 1)
            } else {
              console.log(response.message)
              this.$notify.error({
                title: '错误',
                message: '删除失败',
                duration: 2000
              })
            }
          })
        })
        .catch(_ => {})
    },
    recursiveTree(tree) {
      // 格式化：清掉空的children，name替换成label
      tree.map(item => {
        // item.label = item.Name
        if (item.children) {
          item.children.map(child => {
            // child.label = child.Name
          })
          this.recursiveTree(item.children)
        } else {
          delete item.children
        }
      })
      return tree
    }
  }
}
</script>
<style scoped lang="scss">
.filter-container {
  padding-bottom: 10px;
}
.treeselect-main {
  float: left;
  margin-right: 10px;
	width: 204px;
	line-height: 35px;
	.vue-treeselect__placeholder {
		line-height: 35px;
	}
	.vue-treeselect__control {
		height: 35px;
	}
}

</style>
